<?php

// 查询支付结果
// GET project.notify

namespace app\api\resource\project;
use think\Db;

class Notify
{
    // 方法
    public $_method = ['GET'];
    // 前置
    public $_pre    = ['login'];
    // 描述
    public $_description = '查询支付结果';
    // 参数
    public $_param  = [
        'project_id' => '项目主键',
        'pay_mark'   => '支付标识,cp-初评付款,cx-诚信保证金',
    ];
    // 返回
    public $_return = [
        200 => '查询成功',
        400 => '支付标识不合法',
        404 => '项目不存在',
        422 => '项目主键必须|支付标识必须',
    ];

    public function run(&$request)
    {
        $projectId = $request->get('project_id/d',0);
        $payMark   = $request->get('pay_mark/s','','trim');

        if (0 == $projectId) return [422,'项目主键必须'];
        if ( empty($payMark) ) return [422,'支付标识必须'];

        switch ($payMark) {
            case 'cp':
                $field = 'is_cp_pay';
                break;
            case 'cx':
                $field = 'is_cx_pay';
                break;
            default:
                $field = '';
                break;
        }

        if ( empty($field) ) return [400,'支付标识不合法'];
        
        $ret = Db::name('project')->where('id',$projectId)->field($field)->find();

        if ( is_null($ret) ) return [404,'项目不存在'];

        return [ 200,$ret[$field] ];
    }
}